﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyBlog.Common
{
    public static class LogHelper
    {
        static object locker = new object();
        /// <summary>
        /// 将日志信息写入文件
        /// </summary>
        /// <param name="log"></param>
        public static void WriteLog(string log)
        {
            try
            {
                lock (locker)
                {
                    string fileName = AppDomain.CurrentDomain.BaseDirectory + "/systemLog.txt";
                    FileInfo file = new FileInfo(fileName);
                    StreamWriter writer = null;
                    if (!file.Exists)
                    {
                        writer = new StreamWriter(file.Create(), Encoding.UTF8);
                    }
                    else
                    {
                        writer = new StreamWriter(fileName, true, Encoding.UTF8);
                    }
                    using (writer)
                    {
                        writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "：" + log);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}

